﻿using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Timing;
using Abp.Zero;
using Abp.Zero.Configuration;
using Partner.Surround.Authorization;
using Partner.Surround.Authorization.Roles;
using Partner.Surround.Authorization.Users;
using Partner.Surround.Configuration;
using Partner.Surround.Features;
using Partner.Surround.Localization;
using Partner.Surround.MultiTenancy;
using Partner.Surround.Notifications;
using Partner.Surround.Timing;

namespace Partner.Surround
{
    [DependsOn(typeof(AbpZeroCoreModule))]
    public class SurroundCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;

            // 声明实体类
            Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
            Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
            Configuration.Modules.Zero().EntityTypes.User = typeof(User);

            SurroundLocalizationConfigurer.Configure(Configuration.Localization);

            // 启用多租户
            Configuration.MultiTenancy.IsEnabled = SurroundConsts.MultiTenancyEnabled;

            //配置角色管理
            AppRoleConfig.Configure(Configuration.Modules.Zero().RoleManagement);

            //配置权限管理
            Configuration.Authorization.Providers.Add<SurroundAuthorizationProvider>();

            //配置功能管理
            Configuration.Features.Providers.Add<AppFeatureProvider>();

            //通知定义
            Configuration.Notifications.Providers.Add<AppNotificationProvider>();

            //设置定义
            Configuration.Settings.Providers.Add<AppSettingProvider>();
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(SurroundCoreModule).GetAssembly());
        }

        public override void PostInitialize()
        {
            IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
        }
    }
}
